// src/main/java/cn/zzuli/shopapp/adapter/OrderGoodsAdapterNew.java
package user.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import com.example.xfengshopping.R;

public class OrderGoodsAdapterNew extends RecyclerView.Adapter<OrderGoodsAdapterNew.GoodsViewHolder> {

    private List<GoodsItem> goodsList;

    public OrderGoodsAdapterNew(List<GoodsItem> goodsList) {
        this.goodsList = goodsList;
    }

    @NonNull
    @Override
    public GoodsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_order, parent, false);
        return new GoodsViewHolder(view);
    }

    @Override

    public void onBindViewHolder(@NonNull GoodsViewHolder holder, int position) {
        GoodsItem item = goodsList.get(position);

        // 使用自定义的异步任务下载图片
        new ImageDownloader(holder.ivGoodsImage).execute(item.getImageUrl());
        holder.tvGoodsName.setText(item.getName());
        holder.tvGoodsDescription.setText(item.getDescription());
        holder.tvGoodsPrice.setText("¥" + item.getPrice());
        holder.tvGoodsQuantity.setText("x" + item.getQuantity()); // 显示商品数量
    }

    @Override
    public int getItemCount() {
        return goodsList.size();
    }

    static class GoodsViewHolder extends RecyclerView.ViewHolder {
        ImageView ivGoodsImage;
        TextView tvGoodsName;
        TextView tvGoodsDescription;
        TextView tvGoodsPrice;
        TextView tvGoodsQuantity;

        GoodsViewHolder(View itemView) {
            super(itemView);
            ivGoodsImage = itemView.findViewById(R.id.iv_goods_image);
            tvGoodsName = itemView.findViewById(R.id.tv_goods_name);
            tvGoodsDescription = itemView.findViewById(R.id.tv_goods_description);
            tvGoodsPrice = itemView.findViewById(R.id.tv_goods_price);
            tvGoodsQuantity = itemView.findViewById(R.id.tv_goods_quantity);
        }
    }
}
